package problema021;


public class AmicableNumbers {
	static int[] sumaDivisores;
	static boolean[] amicables;
	
	public static void main(String[] args){
		System.out.println(sumaAmicable());
	}
	
	public static int sumaAmicable(){
		int res = 0;
		sumaDivisores = new int[10000];
		amicables = new boolean[10000];
		for(int i = 0; i < 10000; i++){
			sumaDivisores[i] = sumaDiv(i);
		}
		for(int i = 2; i < 10000; i++){
			if(sumaDivisores[i] > 10000){
				if(i == sumaDiv(sumaDivisores[i])) amicables[i] = true;
				continue;
			}
			if(i != sumaDivisores[i] && sumaDivisores[sumaDivisores[i]] < 10000 && 
					i == sumaDivisores[sumaDivisores[i]]){
				amicables[i] = true;
				amicables[sumaDivisores[i]] = true;
			}
		}
		for(int i = 0; i < 10000; i++){
			if(amicables[i]) res += i;
		}
		/*
		for(int i = 0; i < 10000; i++){
			System.out.print(sumaDivisores[i] + " ");
		}
		System.out.println();
		for(int i = 0; i < 10000; i++){
			System.out.print(amicables[i] + " ");
		}
		System.out.println();*/
		return res;
	}
	
	public static int sumaDiv(int n){
		int res = 0;
		for(int i = 1; i <= n/2; i++){
			if(n % i == 0) res += i;
		}
		return res;
	}
}
